<?php
/* wwwschool project
Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 *
 *  wwwschool project
 *  File : WS3formView.php
 *     
 */  
 
 class WS3_FormView {
 
 private $html = ''; // contains all the html to display
 private $CSSElement = 'main'; // where to display
 private $title = '';
 private $name = 'form';
 private $method = 'POST';
 private $action = 'update'; // which ajax routine to call
 private $buttonReset = false; // display a reset button
 private $hiddenFields = array();
 private $fieldSet = array();
 private $fields = array();
  
  function __construct($css= 'main') {
  $this->CSSElement=$css; // name of the div element which will be used to display the table
   }
   
   function __destruct() {
   }
    // add hidden field 
   function displayResetButton($b=false) {
    $this->buttonReset=$b;
   }
   
   // set title
   function setTitle($t) {
    $this->title=$t;
   }
 
   // set action to call
   function setAction($a) {
    $this->action=$a;
   }
      // add paragraph of text
   function addParagraph($value) {
    array_push($this->fields, array('type'=>'paragraph','value'=>$value) );
   }
   
   // add fieldset with legend
   function addFieldset($name,$legend="") {
    array_push($this->fieldSet, array('name'=>$name,'legend'=>$legend) );
   }
   // add hidden field 
   function addHiddenField($name,$value) {
    array_push($this->hiddenFields, array('name'=>$name,'value'=>$value) );
   }
    // add password field 
   function addPasswordField($label='',$id='',$name='',$value='') {
    array_push($this->fields, array('type'=>'password','label'=>$label,'id'=>$id,'name'=>$name,'value'=>$value) );
   }   
    // add text field 
   function addTextField($label='',$id='',$name='',$value='') {
    array_push($this->fields, array('type'=>'text','label'=>$label,'id'=>$id,'name'=>$name,'value'=>$value) );
   }
   
   // add textarea 
   function addTextArea($label='',$id='',$name='',$value='') {
    array_push($this->fields, array('type'=>'textarea','label'=>$label,'id'=>$id,'name'=>$name,'value'=>$value) );
   }
   
   // add select
   function addSelect($label='',$id='',$name='',$options='',$selected='') {
     $s="<select id=\"$id\" name=\"$name\">";
     foreach ($options as $k=>$v)
     {
      $isSelected="";
      if ($k == $selected) $isSelected=" selected=\"selected\"";
      $s.="<option value=\"$k\"$isSelected>$v</option>";
     }
     $s.="</select>";
      array_push($this->fields, array('type'=>'select','label'=>$label,'html'=>$s ) );
   }
   
   // displays the hidden fields
   function displayHiddenFields() {
     foreach($this->hiddenFields as $a){
      $this->html.="<input type=\"hidden\" name=\"".$a['name']."\" value=\"".$a['value']."\" />";
     }
   }
  
   // displays the fieldsets and legend
   function displayFieldsets(){
    foreach($this->fieldSet as $a){
    $this->html.="<fieldset>";
    if (isset($a['legend']) )$this->html.="<legend>".$a['legend']."</legend>";
    $this->html.="</fieldset>";
    }
  } 
   // displays the fields
   function displayFields() {
   
     foreach($this->fields as $a){
     switch ($a['type'])
     {
        case ('paragraph'):
        $this->html.="<p>".$a['value']."</p>";
        break;       
        case ('radio'):
        $this->html.="<p><label>".$a['label']."</label><input type=\"checkbox\" id=\"".$a['id']."\" name=\"".$a['name']."\" value=\"".$a['value']."\" /></p>";
        break;     
        case ('checkbox'):
        $this->html.="<p><label>".$a['label']."</label><input type=\"checkbox\" id=\"".$a['id']."\" name=\"".$a['name']."\" value=\"".$a['value']."\" /></p>";
        break;
      case ('text'):
        $this->html.="<p><label>".$a['label']."</label><input type=\"text\" id=\"".$a['id']."\" name=\"".$a['name']."\" value=\"".$a['value']."\" /></p>";
        break;
      case ('password'):
        $this->html.="<p><label>".$a['label']."</label><input type=\"password\" id=\"".$a['id']."\" name=\"".$a['name']."\" value=\"".$a['value']."\" /></p>";        
        break;
      case ('textarea'):
        $this->html.="<p><label>".$a['label']."</label><textarea name=\"".$a['name']."\" id=\"".$a['id']."\">".$a['value']."</textarea></p>";
        break;
      case ('select'):
        $this->html.="<p><label>".$a['label']."</label>".$a['html']."</p>";
        break;                
     }
      
     }
      
   } 
   
   //
   function prepareOutput()
   { 
    $this->displayHiddenFields();
     $this->displayFieldsets();
     $this->html.="<fieldset>";
     $this->displayFields();
     
     $this->html.="</fieldset><fieldset><legend>Options</legend><p>";
     if ($this->buttonReset) $this->html.="<input type=\"reset\" class=\"buttonCancel\" /></p>";
     $this->html.="<input type=\"submit\"  class=\"buttonSubmit\" /></p>";
     $this->html.="</fieldset>";
     $this->html.="</form></div>"; 
     }
   
      //display the form for HTML output
  function displayHTML() {
     if ( $this->title>"" ) $this->html.="<h2>".$this->title."</h2>";
     $this->html.= "<div id=\"formEdit\"><form name=\"".$this->name."\" action=\"".$this->action."\" method=\"".$this->method."\">";
     
    $this->prepareOutput();
    return $this->html;
   } 
   
   //display the form for java output
   function display() {
     if ( $this->title>"" ) $this->html.="<h2>".$this->title."</h2>";
     $this->html.= "<div id=\"formEdit\"><form name=\"".$this->name."\" onsubmit=\"ajaxForm(\'".$this->action."\',\'".$this->name."\');return false;\" method=\"".$this->method."\">";    
     $this->prepareOutput();

    return "document.getElementById('main').innerHTML = '".$this->html."';";
   } 
   
 } // end class
 
 ?>